﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;
using Eqwww.Data;
using Eqwww.Domain.Models.SystemManage;
using Eqwww.App.SystemManage;
using Eqwww.App.Authorize;
using Eqwww.Code;

namespace Eqwww.Web.Areas.SystemManage.Controllers
{
    [Area("SystemManage")]
    public class JobController : ControllerBase
    {
        private JobApp _jobApp;
        private AuthorizeApp _authorizeApp;

        public JobController(JobApp jobApp, AuthorizeApp authorizeApp)
        {
            _jobApp = jobApp;
            _authorizeApp = authorizeApp;
        }

        [AuthFilter]
        public IActionResult Index()
        {
            var currentModuleCode = "SystemManage_Job";
            ViewData["SelectedButtons"] = _authorizeApp.GetAuthorizedModuleButtons(User, currentModuleCode, ModuleButtonPosition.Selected);
            ViewData["InitialButtons"] = _authorizeApp.GetAuthorizedModuleButtons(User, currentModuleCode, ModuleButtonPosition.Initial);


            return View();
        }

        public IActionResult IndexGridJson(string keyword, Pagination pagination)
        {
            var data = new
            {
                rows = _jobApp.GetPagedList(keyword, pagination),
                total = pagination.total,
                page = pagination.page,
                records = pagination.records
            };
            return Content(data.ToJson());
        }

        [AuthFilter]
        public async Task<IActionResult> Details(string id)
        {
            if (id == null)
            {
                return NotFound();
            }

            var jobModel = await _jobApp.GetAsync(id);
            if (jobModel == null)
            {
                return NotFound();
            }

            return View(jobModel);
        }

        [AuthFilter]
        public IActionResult Create()
        {
            
            return View();
        }

        
        [HttpPost]
        [ExceptionFilter]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Create([Bind("Id,Code,Name,SortCode")] JobModel jobModel)
        {
            if (ModelState.IsValid)
            {
                return Json(await _jobApp.CreateAsync(jobModel));
            }

            return Error(ModelState.ErrorCount.ToString());
        }

        [AuthFilter]
        public async Task<IActionResult> Edit(string id)
        {
            if (id == null)
            {
                return NotFound();
            }

            var jobModel = await _jobApp.GetAsync(id);
            if (jobModel == null)
            {
                return NotFound();
            }
           
            return View(jobModel);
        }

        
        [HttpPost]
        [ExceptionFilter]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Edit(string id, [Bind("Id,Code,Name,SortCode")] JobModel jobModel)
        {
            if (id != jobModel.Id)
            {
                return NotFound();
            }

            if (ModelState.IsValid)
            {
                try
                {
                    return Json(await _jobApp.UpdateAsync(jobModel));
                }
                catch (Exception ex)
                {
                    return Error(ex.Message);
                }
                
            }
            
            return Error(ModelState.ErrorCount.ToString());
        }

        

       
        [HttpPost, ActionName("Delete")]
        [ExceptionFilter]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> DeleteConfirmed(string id)
        {
            return Json(await _jobApp.RemoveAsync(id));
        }

        public IActionResult TreeCollapse()
        {
            ViewData["Items"] = _jobApp.GetTreeCollapseItems();
            return View();
        }


    }
}
